﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Security.Principal;
using System.Diagnostics;
using System.ComponentModel;

namespace TrafficSplitter
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            if (!pricipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                RunElevated(Application.ExecutablePath);
            }
            else
            {
#if !DEBUG
                try
                {
#endif
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Main());
#if !DEBUG
                }
                catch (Exception ex)
                {
                    String msg = "Error: " + ex.Message + "\n\nStackTrace: " + ex.StackTrace;
                    Clipboard.SetText(msg);
                    MessageBox.Show(msg + "\n\nThis message has been copied to the clipboard.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
#endif
            }
        }


        static void RunElevated(String filename)
        {
            ProcessStartInfo processInfo = new ProcessStartInfo();
            processInfo.Verb = "runas";
            processInfo.FileName = filename;
            try
            {
                Process.Start(processInfo);
            }
            catch (Win32Exception)
            {
                // Do nothing
            }
        }
    }
}
